<?php

/******************************************
 * File:			lib_basic.php
 * Version:			1.0.1
 * Last modified:	25/10/2009
 * Last modifier:	Hugo Lavigne
 *****************************************/

Function redirect($path, $time=1)
{
	echo "<script language=JavaScript>setTimeout(\"document.location = '$path'\", $time)</script>";
	return;
}

Function is_email($mail)
{
  $condition = '#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,5}$#';
  if(preg_match($condition, $mail))
    return (0);
  else
    return (-1);
}

Function create_id($size)
{
	$chaine = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	srand((double)microtime()*1000000);
	for($i=0; $i<$size; $i++){
	  @$id .= $chaine[rand()%strlen($chaine)];}
	return($id);
}

Function get_page_name($url)
{
  $res = str_to_word_tab($url, "/");
  $len = count($res) - 1;
  $res = str_to_word_tab($res[$len], ".");
  $len = count($res) - 2;
  $_SESSION['page'] = $res[$len];
  return ($res[$len]);
}

Function show_page($start, $limit, $table, $id, $sendto)
{
	$connection = dbconnect();
	$req = dbquery("SELECT $id FROM $table WHERE 1", $connection);
	
	if (!@mysql_num_rows($req))
	{
		Print("Page <b>1</b> sur <b>1</b>");
		mysql_close($connection);
		return(-1);
	}
	
	$nb_res = mysql_num_rows($req);
	if (($nb_res % $limit) > 0)
		$nb_pages = round(($nb_res / $limit), 0) + 1;	
	else
		$nb_pages = ($nb_res / $limit);
		
	if ($nb_pages == 0)
			$nb_pages = 1;
		
	if (($start % $limit) > 0)
		$page = round(($start / $limit), 0) + 1;
	else
		$page = $start / $limit;
	
	if ($page == 0)
		$page = 1;
	
	Print("Page <b>$page</b> sur <b>$nb_pages</b>");
		
	if (($start + $limit) > $nb_res)
	{
		if (($stp = ($start - $limit)) >= 0)
			Print(" | <a href=\"$sendto=$stp\">< Pr&eacute;c&eacute;dente</a>");
	}
	elseif (($sts = ($start + $limit)) <= $nb_res)
	{
		if (($stp = ($start - $limit)) >= 0)
			Print(" | <a href=\"$sendto=$stp\">< Pr&eacute;c&eacute;dente</a> - <a href=\"$sendto=$sts\">Suivante ></a>");
		else
			Print(" | <a href=\"$sendto=$sts\">Suivante ></a>");
	}
	return;
}

Function show_page_search($start, $limit, $query, $sendto)
{
	$connection = dbconnect();
	$req = dbquery( $query, $connection);
	
	if (!@mysql_num_rows($req))
	{
		Print("Page <b>1</b> sur <b>1</b>");
		mysql_close($connection);
		return(-1);
	}
	
	$nb_res = mysql_num_rows($req);
	if (($nb_res % $limit) > 0)
		$nb_pages = round(($nb_res / $limit), 0) + 1;	
	else
		$nb_pages = $nb_res / $limit;
		
	if ($nb_pages == 0)
			$nb_pages = 1;
		
	if (($start % $limit) > 0)
		$page = round(($start / $limit), 0) + 1;
	else
		$page = $start / $limit;
	
	if ($page == 0)
		$page = 1;
	
	Print("Page <b>$page</b> sur <b>$nb_pages</b>");
		
	if (($start + $limit) > $nb_res)
	{
		if (($stp = ($start - $limit)) >= 0)
			Print(" | <a href=\"$sendto=$stp\">< Pr&eacute;c&eacute;dente</a>");
	}
	elseif (($sts = ($start + $limit)) <= $nb_res)
	{
		if (($stp = ($start - $limit)) >= 0)
			Print(" | <a href=\"$sendto=$stp\">< Pr&eacute;c&eacute;dente</a> - <a href=\"$sendto=$sts\">Suivante ></a>");
		else
			Print(" | <a href=\"$sendto=$sts\">Suivante ></a>");
	}
	return;
}

?>